package cn.easytogo.util;

import java.util.*;

// Referenced classes of package cn.com.victorysoft.eaf.util:
//            StringUtil, JSONUtil

public class TreeUtil
{

    public TreeUtil()
    {
    }

    public static String getTreeJSON(List list, String s, String s1, String s2, String s3, boolean flag, String as[])
    {
        return getTreeJSON(list, s, s1, s2, s3, flag, as, false, null);
    }

    public static String getTreeJSON(List list, String s, String s1, String s2, String s3, boolean flag, String as[], Object aobj[])
    {
        return getTreeJSON(list, s, s1, s2, s3, flag, as, false, aobj);
    }

    public static String getTreeJSON(List list, String pidValue, String idFieldName, String textFieldName, String pidFieldName, boolean flag, String as[], boolean flag1, 
            Object aobj[])
    {
        if(null == list)
            return "{}";
        if(null == pidValue || pidValue.trim().equals(""))
            pidValue = "0";
        ArrayList arraylist = new ArrayList();
        for(int i = 0; i < list.size(); i++)
        {
            Map map = (Map)list.get(i);
            if(pidValue.equals(map.get(idFieldName)) || !pidValue.trim().equals(String.valueOf(map.get(pidFieldName))))
                continue;
            HashMap hashmap1 = new HashMap();
            hashmap1.put("id", map.get(idFieldName));
            hashmap1.put("text", map.get(textFieldName));
            if(flag)
                hashmap1.put("open", "1");
            if(flag1)
                hashmap1.put("child", "1");
            if(null != aobj && aobj.length > 0 && Arrays.asList(aobj).contains(map.get(idFieldName)))
                hashmap1.put("checked", "1");
            if(null != as)
            {
                ArrayList arraylist1 = new ArrayList();
                for(int j = 0; j < as.length; j++)
                {
                    HashMap hashmap2 = new HashMap();
                    String s4 = as[j];
                    hashmap2.put("name", s4);
                    hashmap2.put("content", StringUtil.null2blank(map.get(s4)));
                    arraylist1.add(hashmap2);
                }

                hashmap1.put("userdata", arraylist1);
            }
            List list1 = getChildNodes(list, String.valueOf(map.get(idFieldName)), idFieldName, textFieldName, pidFieldName, flag, as, aobj);
            if(!list1.isEmpty())
                hashmap1.put("item", list1);
            arraylist.add(hashmap1);
        }

        HashMap hashmap = new HashMap();
        hashmap.put("id", pidValue);
        hashmap.put("item", arraylist);
        return JSONUtil.object2json(hashmap);
    }

    private static List getChildNodes(List list, String s, String s1, String s2, String s3, boolean flag, String as[], Object aobj[])
    {
        ArrayList arraylist = new ArrayList();
        for(int i = 0; i < list.size(); i++)
        {
            Map map = (Map)list.get(i);
            if(!s.trim().equals(String.valueOf(map.get(s3))))
                continue;
            HashMap hashmap = new HashMap();
            hashmap.put("id", map.get(s1));
            hashmap.put("text", map.get(s2));
            if(flag)
                hashmap.put("open", "1");
            if(null != aobj && aobj.length > 0 && Arrays.asList(aobj).contains(map.get(s1)))
                hashmap.put("checked", "1");
            if(null != as)
            {
                ArrayList arraylist1 = new ArrayList();
                for(int j = 0; j < as.length; j++)
                {
                    HashMap hashmap1 = new HashMap();
                    String s4 = as[j];
                    hashmap1.put("name", s4);
                    hashmap1.put("content", StringUtil.null2blank(map.get(s4)));
                    arraylist1.add(hashmap1);
                }

                hashmap.put("userdata", arraylist1);
            }
            List list1 = getChildNodes(list, String.valueOf(map.get(s1)), s1, s2, s3, flag, as, aobj);
            if(!list1.isEmpty())
                hashmap.put("item", list1);
            arraylist.add(hashmap);
        }

        return arraylist;
    }

    public static void main(String args[])
    {
        ArrayList arraylist = new ArrayList();
        HashMap hashmap = new HashMap();
        hashmap.put("id", "1");
        hashmap.put("caption", "\u8282\u70B91");
        hashmap.put("pid", "0");
        hashmap.put("f1", "\u9644\u52A01");
        hashmap.put("f2", "\u9644\u52A02");
        arraylist.add(hashmap);
        HashMap hashmap1 = new HashMap();
        hashmap1.put("id", "1001");
        hashmap1.put("caption", "\u8282\u70B91-1");
        hashmap1.put("pid", "1");
        arraylist.add(hashmap1);
        HashMap hashmap2 = new HashMap();
        hashmap2.put("id", "1002");
        hashmap2.put("caption", "\u8282\u70B91-2");
        hashmap2.put("pid", "1");
        arraylist.add(hashmap2);
        String s = getTreeJSON(arraylist, null, "id", "caption", "pid", false, new String[] {
            "f1", "f2"
        }, true, null);
        System.out.println(s);
    }
}